Foreign Property Example

This example shows how the Foreign property can indicate which Index objects in a TableDef are foreign key indexes. Such indexes are created by the Microsoft Jet database engine when a Relation is created. The default name for the foreign key indexes is the name of the primary table plus the name of the foreign table. The ForeignOutput function is required for this procedure to run.

Sub ForeignX()

   Dim dbsNorthwind As Database

   Set dbsNorthwind = OpenDatabase("Northwind.mdb")

   With dbsNorthwind
      ' Print report on foreign key indexes from two 
      ' TableDef objects and a QueryDef object.
      ForeignOutput .TableDefs!Products
      ForeignOutput .TableDefs!Orders
      ForeignOutput .TableDefs![Order Details]

      .Close
   End With

End Sub

Function ForeignOutput(tdfTemp As TableDef)

   Dim idxLoop As Index

   With tdfTemp
      Debug.Print "Indexes in " & .Name & " TableDef"
      ' Enumerate the Indexes collection of the specified 
      ' TableDef object.
      For Each idxLoop In .Indexes
         Debug.Print "  " & idxLoop.Name
         Debug.Print "    Foreign = " & idxLoop.Foreign
      Next idxLoop
   End With

End Function